package com.loongtech.bi.action.system;

import com.loongtech.bi.entity.system.EntityMonitorGroupConfig;
import com.loongtech.bi.entity.system.EntityMonitorUserConfig;
import com.loongtech.bi.entity.system.EntityMonitorUserGroup;
import com.loongtech.bi.manager.system.BiMonitorGroupConfigManager;
import com.loongtech.bi.manager.system.BiMonitorUserConfigManager;
import com.loongtech.bi.manager.system.BiMonitorUserGroupManager;
import org.primefaces.model.DualListModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 单个角色用户设置
 * 
 * @author xckuang
 * @date 2014-10-08
 */
@Controller
@Scope("view")
public class BiMonitorUserGroupAction implements Serializable {
	private static final long serialVersionUID = 1L;
	@Resource
	private BiMonitorGroupConfigManager groupManager;
    @Resource
	private BiMonitorUserGroupManager userGroupManager;
    @Resource
	private BiMonitorUserConfigManager userManager;

	private DualListModel<EntityMonitorUserConfig> users;
	private Map<Integer, EntityMonitorGroupConfig> groupMap = new HashMap<>();
	private int groupId;

	@Autowired
	public void init() {
		List<EntityMonitorUserConfig> userTarget = new ArrayList<>();
		List<EntityMonitorUserConfig> userSource = new ArrayList<>();
		EntityMonitorGroupConfig res = groupManager.get(groupId);
		if (null == res) {
			userTarget.clear();
			userSource.clear();
			userSource.addAll(userManager.findAll());
		} else {
			List<Integer> groupList = userGroupManager.findByQuery("select ug.userId from EntityMonitorUserGroup ug where ug.groupId = ?", groupId);
			if (groupList != null && !groupList.isEmpty()) {
				for (Integer val : groupList) {
					EntityMonitorUserConfig entity = userManager.getByQuery("from EntityMonitorUserConfig uc where uc.id = ?", val);
					userTarget.add(entity);
				}
			}
//			for (EntityMonitorUserConfig e: userTarget) {
//				System.out.println(e.getWeChat());
//			}
			userSource = userManager.findAll();
			userSource.removeAll(userTarget);
		}
		users = new DualListModel<>(userSource, userTarget);
		for (EntityMonitorGroupConfig group : groupManager.findAll()) {
			groupMap.put(group.getId(), group);
		}
	}

	/**
	 * 保存分组用户信息
	 */
	public void save() {
		FacesContext context = FacesContext.getCurrentInstance();
		EntityMonitorGroupConfig res = groupManager.get(groupId);
		if (null == res) {
			context.addMessage(null, new FacesMessage("提示", "修改分组用户列表失败，分组不存在， groupId=" + res));
			return;
		}

		// 取target中用户id放入idlist
		List<Integer> idlist = new ArrayList<>();
		if (null != users && null != users.getTarget() && !users.getTarget().isEmpty()) {
			for (EntityMonitorUserConfig user : users.getTarget()) {
				if (user.getId() != null) {
					idlist.add(user.getId());
				}
			}
		}
		// 取数据库中的用户id放入dblist
		List<Integer> dblist = new ArrayList<>();
		List<EntityMonitorUserGroup> list = userGroupManager.findByQuery("from EntityMonitorUserGroup where groupId = ?", groupId);
		if (null != list && !list.isEmpty()) {
			for (EntityMonitorUserGroup info : list) {
				if (info.getUserId() != null) {
					dblist.add(info.getUserId());
				}
			}
		}
		// 删
		if (!dblist.isEmpty()) {
			for (Integer info : dblist) {
				if (!idlist.contains(info)) {
					Integer userGroupId = userGroupManager.getByQuery("select e.id from EntityMonitorUserGroup e where userId = ? and groupId = ?", info, groupId);
					userGroupManager.removeById(userGroupId);
				}
			}
		}
		//增
		if (!idlist.isEmpty()) {
			for (Integer info : idlist) {
				if (!dblist.contains(info)) {
					EntityMonitorUserGroup entity = new EntityMonitorUserGroup();
					entity.setGroupId(groupId);
					entity.setUserId(info);
					userGroupManager.persist(entity);
				}
			}
		}
		context.addMessage(null, new FacesMessage("提示", "修改分组用户信息列表成功，分组名称为=" + res.getGroupName()));
		init();
	}

	public DualListModel<EntityMonitorUserConfig> getUsers() {
		return users;
	}

	public void setUsers(DualListModel<EntityMonitorUserConfig> users) {
		this.users = users;
	}

	public Map<Integer, EntityMonitorGroupConfig> getGroupMap() {
		return groupMap;
	}

	public void setGroupMap(Map<Integer, EntityMonitorGroupConfig> groupMap) {
		this.groupMap = groupMap;
	}

	public int getGroupId() {
		return groupId;
	}

	public void setGroupId(int groupId) {
		this.groupId = groupId;
	}

}